/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * An abstract class for the main user interface used in both editor and game mode
 */

#include "GameInterfaceItemAndObjectContainer.h"
#include "GameInterfacePhysicObjectButton.h"
#include "../../MainEngine.h"
#include "../../Resources.h"
#include "../../Common.h"

using namespace cocos2d;

// default constructor
GameInterfaceItemAndObjectContainer::GameInterfaceItemAndObjectContainer( cocos2d::CCLayer *layer, MainEngine *mainEngine )
	: GameInterfaceObject( layer ), mMainEngine( mainEngine ), mBackgroundSprite( 0 ), mMenu( 0 ) {

	// create interface background and set it's scale and position etc
	mBackgroundSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_INTERFACE_BACKGROUND ) );
	mBackgroundSprite->setScaleX( RATEX * 12.0f );
	mBackgroundSprite->setScaleY( RATEY * 32.0f );
	mBackgroundSprite->setAnchorPoint( ccp( 1.0f, 0.0f ) );
	mBackgroundSprite->setPosition( ccp( WIN_WIDTH, 0.0f ) );
	mLayer->addChild( mBackgroundSprite, Z_LAYER_BACKGROUND );

	// init menu
	mMenu = CCMenu::node();
	mMenu->setPosition( 0.0f, 0.0f );
	mLayer->addChild( mMenu );

	// add exit item
	cocos2d::CCMenuItemImage *exitItem = cocos2d::CCMenuItemImage::itemFromNormalImage( 
		getResource( Textures::GAME_INTERFACE_EXIT ), getResource( Textures::GAME_INTERFACE_EXIT_PRESSED ), 
		this, menu_selector( GameInterfaceItemAndObjectContainer::exitCallback ) );
	exitItem->setScale( RATEX );
	exitItem->setPosition( ccp( WIN_WIDTH * 0.95f, WIN_HEIGHT * 0.07f ) );
	mMenu->addChild( exitItem, Z_LAYER_INTERFACE );

	// add help item
	cocos2d::CCMenuItemImage *helpItem = cocos2d::CCMenuItemImage::itemFromNormalImage( 
		getResource( Textures::GAME_INTERFACE_HELP ), getResource( Textures::GAME_INTERFACE_HELP_PRESSED ), 
		this, menu_selector( GameInterfaceItemAndObjectContainer::helpCallback ) );
	helpItem->setScale( RATEX );
	helpItem->setPosition( ccp( WIN_WIDTH * 0.865f, WIN_HEIGHT * 0.07f ) );
	mMenu->addChild( helpItem, Z_LAYER_INTERFACE );
}

// default destructor
GameInterfaceItemAndObjectContainer::~GameInterfaceItemAndObjectContainer() {
	mLayer->removeChild( mBackgroundSprite, true );
	mLayer->removeChild( mMenu, true );
}

// update function of the object
void GameInterfaceItemAndObjectContainer::update( cocos2d::ccTime dt ) {
	for( unsigned int i = 0; i < mObjectButtons.size(); ++i )
		mObjectButtons.at( i )->update( dt );
}

// common callback functions

// exit the game or the editor
void GameInterfaceItemAndObjectContainer::exitCallback( cocos2d::CCObject *pSender ) {
	mMainEngine->sceneFinishedExecution();
}

// help button
void GameInterfaceItemAndObjectContainer::helpCallback( cocos2d::CCObject *pSender ) {
}

// get the object buttons used
std::vector< GameInterfacePhysicObjectButton* > &GameInterfaceItemAndObjectContainer::getObjectButtons() {
	return mObjectButtons;
}

// create and return the physics body of the wanted element
b2Body *GameInterfaceItemAndObjectContainer::createNewItem( GameCamera *camera, PhysicObjectItems type, b2Vec2 pos ) {
	return 0;
}
